package com.guigu.qingcheng.orderservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.orderservice.mapper.TbCategoryReportMapper;
import com.guigu.qingcheng.orderservice.service.TbCategoryReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.qingcheng.pojo.order.TbCategoryReport;
import com.guigu.qingcheng.utils.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class TbCategoryReportServiceImpl extends ServiceImpl<TbCategoryReportMapper, TbCategoryReport> implements TbCategoryReportService {

    @Override
    public R findPage(Integer page, Integer size, Map<String, Object> searchMap) {
        Page<TbCategoryReport> reportPage = new Page<>(page,size);

        QueryWrapper<TbCategoryReport> wrapper = new QueryWrapper<>();
        if (searchMap != null) {
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id")))
                wrapper.eq("id",searchMap.get("id"));
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name")))
                wrapper.like("name",searchMap.get("name"));
        }
        baseMapper.selectPage(reportPage,wrapper);
        return R.ok().data("total",reportPage.getTotal()).data("rows",reportPage.getRecords());
    }

    @Override
    public List<TbCategoryReport> categoryReports(LocalDate date) {
       return baseMapper.categoryReports(date);
    }

    @Override
    @Transactional
    public void createData() {
        LocalDate localDate = LocalDate.now().minusDays(1);
        List<TbCategoryReport> tbCategoryReports = baseMapper.categoryReports(localDate);
        for (TbCategoryReport categoryReport:tbCategoryReports) {
            baseMapper.insert(categoryReport);
        }
    }

    @Override
    public List<Map> category1Count(String date1, String date2) {
        return baseMapper.category1Count(date1,date2);
    }
}
